﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class ReceiptValidationDA : IReceiptValidationDA
    {
        private string COMMODITY_NOT_ON_ASN = "@COMMODITY_NOT_ON_ASN";
        private string COMMODITY_NOT_ON_PO = "@COMMODITY_NOT_ON_PO";
        private string CREATED_BY = "@CREATED_BY";
        private string DEFAULT_FLAG = "@DEFAULT_FLAG";
        private string DESCR = "@DESCR";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string IS_QTY_VALIDATION = "@IS_QTY_VALIDATION";
        private string OVERAGE_PERCENT = "@OVERAGE_PERCENT";
        private string RECEIPT_VALIDATION_ID = "@RECEIPT_VALIDATION_ID";
        private string RECEIPT_WITHOUT_ASN = "@RECEIPT_WITHOUT_ASN";
        private string RECEIPT_WITHOUT_LPN = "@RECEIPT_WITHOUT_LPN";
        private string RECEIPT_WITHOUT_PO = "@RECEIPT_WITHOUT_PO";
        private string SQL_CHECK_RECEIPT_VALIDATION_ID_UNIQUE = " SELECT COUNT(1) FROM @_@RECEIPT_VALIDATION WHERE RECEIPT_VALIDATION_ID = @RECEIPT_VALIDATION_ID ";
        private string SQL_DELETE_RECEIPT_VALIDATION = " DELETE FROM @_@RECEIPT_VALIDATION WHERE RECEIPT_VALIDATION_ID = @RECEIPT_VALIDATION_ID ";
        private string SQL_INSERT_RECEIPT_VALIDATION = " INSERT INTO @_@RECEIPT_VALIDATION ( WH_ID, RECEIPT_VALIDATION_ID, DEFAULT_FLAG, RECEIPT_WITHOUT_PO, RECEIPT_WITHOUT_LPN, RECEIPT_WITHOUT_ASN, COMMODITY_NOT_ON_PO, COMMODITY_NOT_ON_ASN, IS_QTY_VALIDATION, OVERAGE_PERCENT, DESCR, IS_ACTIVE, SYSTEM_CREATED, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @RECEIPT_VALIDATION_ID, @DEFAULT_FLAG, @RECEIPT_WITHOUT_PO, @RECEIPT_WITHOUT_LPN, @RECEIPT_WITHOUT_ASN, @COMMODITY_NOT_ON_PO, @COMMODITY_NOT_ON_ASN, @IS_QTY_VALIDATION, @OVERAGE_PERCENT, @DESCR, @IS_ACTIVE, @SYSTEM_CREATED, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_RECEIPT_VALIDATION = " SELECT  ROWID, WH_ID, RECEIPT_VALIDATION_ID, DEFAULT_FLAG, RECEIPT_WITHOUT_PO, RECEIPT_WITHOUT_LPN, RECEIPT_WITHOUT_ASN, COMMODITY_NOT_ON_PO, COMMODITY_NOT_ON_ASN, IS_QTY_VALIDATION, OVERAGE_PERCENT, DESCR, IS_ACTIVE, SYSTEM_CREATED, TS, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@RECEIPT_VALIDATION ";
        private string SQL_UPDATE_RECEIPT_VALIDATION = " UPDATE @_@RECEIPT_VALIDATION SET WH_ID = @WH_ID, DEFAULT_FLAG = @DEFAULT_FLAG, RECEIPT_WITHOUT_PO = @RECEIPT_WITHOUT_PO, RECEIPT_WITHOUT_LPN = @RECEIPT_WITHOUT_LPN, RECEIPT_WITHOUT_ASN = @RECEIPT_WITHOUT_ASN, COMMODITY_NOT_ON_PO = @COMMODITY_NOT_ON_PO, COMMODITY_NOT_ON_ASN = @COMMODITY_NOT_ON_ASN, IS_QTY_VALIDATION = @IS_QTY_VALIDATION, OVERAGE_PERCENT = @OVERAGE_PERCENT, DESCR = @DESCR, IS_ACTIVE = @IS_ACTIVE, SYSTEM_CREATED = @SYSTEM_CREATED, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE RECEIPT_VALIDATION_ID = @RECEIPT_VALIDATION_ID  ";
        private string SYSTEM_CREATED = "@SYSTEM_CREATED";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckReceiptValidationIDUnique(string receiptValidationID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@RECEIPT_VALIDATION_ID", receiptValidationID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_RECEIPT_VALIDATION_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckReceiptValidationIDUnique(DataBase dataBase, DbTransaction tran, string receiptValidationID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@RECEIPT_VALIDATION_ID", receiptValidationID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_RECEIPT_VALIDATION_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteReceiptValidation(List<string> receiptValidationID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < receiptValidationID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@RECEIPT_VALIDATION_ID", receiptValidationID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_RECEIPT_VALIDATION.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteReceiptValidation(DataBase dataBase, DbTransaction tran, List<string> receiptValidationID, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int j = 0; j < receiptValidationID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@RECEIPT_VALIDATION_ID", receiptValidationID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_RECEIPT_VALIDATION.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllReceiptValidation(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_RECEIPT_VALIDATION.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllReceiptValidation(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_RECEIPT_VALIDATION.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public ReceiptValidationInfo GetReceiptValidationByID(string receiptvalidationID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_RECEIPT_VALIDATION.Replace("@_@", whLoginID) + " WHERE RECEIPT_VALIDATION_ID = @RECEIPT_VALIDATION_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@RECEIPT_VALIDATION_ID", receiptvalidationID) };
            ReceiptValidationInfo receiptValidationInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    receiptValidationInfo = new ReceiptValidationInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["RECEIPT_VALIDATION_ID"].ToString(), reader["DEFAULT_FLAG"].ToString(), reader["RECEIPT_WITHOUT_PO"].ToString(), reader["RECEIPT_WITHOUT_LPN"].ToString(), reader["RECEIPT_WITHOUT_ASN"].ToString(), reader["COMMODITY_NOT_ON_PO"].ToString(), reader["COMMODITY_NOT_ON_ASN"].ToString(), reader["IS_QTY_VALIDATION"].ToString(), (reader["OVERAGE_PERCENT"].ToString() != "") ? decimal.Parse(reader["OVERAGE_PERCENT"].ToString()) : 0M, reader["DESCR"].ToString(), reader["IS_ACTIVE"].ToString(), reader["SYSTEM_CREATED"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return receiptValidationInfo;
        }

        public ReceiptValidationInfo GetReceiptValidationByID(DataBase dataBase, DbTransaction tran, string receiptvalidationID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_RECEIPT_VALIDATION.Replace("@_@", whLoginID) + " WHERE RECEIPT_VALIDATION_ID = @RECEIPT_VALIDATION_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@RECEIPT_VALIDATION_ID", receiptvalidationID) };
            ReceiptValidationInfo receiptValidationInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                receiptValidationInfo = new ReceiptValidationInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["RECEIPT_VALIDATION_ID"].ToString(), reader["DEFAULT_FLAG"].ToString(), reader["RECEIPT_WITHOUT_PO"].ToString(), reader["RECEIPT_WITHOUT_LPN"].ToString(), reader["RECEIPT_WITHOUT_ASN"].ToString(), reader["COMMODITY_NOT_ON_PO"].ToString(), reader["COMMODITY_NOT_ON_ASN"].ToString(), reader["IS_QTY_VALIDATION"].ToString(), (reader["OVERAGE_PERCENT"].ToString() != "") ? decimal.Parse(reader["OVERAGE_PERCENT"].ToString()) : 0M, reader["DESCR"].ToString(), reader["IS_ACTIVE"].ToString(), reader["SYSTEM_CREATED"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return receiptValidationInfo;
        }

        public DataSet GetReceiptValidationByQueryList(List<string> sqlWhere, ReceiptValidationQueryEntity receiptvalidationQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_RECEIPT_VALIDATION.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!receiptvalidationQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, receiptvalidationQuery.CurrentPage, receiptvalidationQuery.PageSize, receiptvalidationQuery.SortField, receiptvalidationQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetReceiptValidationByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, ReceiptValidationQueryEntity receiptvalidationQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_RECEIPT_VALIDATION.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, receiptvalidationQuery.CurrentPage, receiptvalidationQuery.PageSize, receiptvalidationQuery.SortField, receiptvalidationQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertReceiptValidation(ReceiptValidationInfo receiptValidationInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                receiptValidationInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_ReceiptValidation_Parameters(receiptValidationInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_RECEIPT_VALIDATION.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertReceiptValidation(DataBase dataBase, DbTransaction tran, ReceiptValidationInfo receiptValidationInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                receiptValidationInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_ReceiptValidation_Parameters(receiptValidationInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_RECEIPT_VALIDATION.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_ReceiptValidation_Parameters(ReceiptValidationInfo receiptValidationInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.RECEIPT_VALIDATION_ID, SqlDbType.VarChar, 20), new SqlParameter(this.DEFAULT_FLAG, SqlDbType.VarChar, 10), new SqlParameter(this.RECEIPT_WITHOUT_PO, SqlDbType.VarChar, 10), new SqlParameter(this.RECEIPT_WITHOUT_LPN, SqlDbType.VarChar, 10), new SqlParameter(this.RECEIPT_WITHOUT_ASN, SqlDbType.VarChar, 10), new SqlParameter(this.COMMODITY_NOT_ON_PO, SqlDbType.VarChar, 10), new SqlParameter(this.COMMODITY_NOT_ON_ASN, SqlDbType.VarChar, 10), new SqlParameter(this.IS_QTY_VALIDATION, SqlDbType.VarChar, 10), new SqlParameter(this.OVERAGE_PERCENT, SqlDbType.Decimal, 13), new SqlParameter(this.DESCR, SqlDbType.VarChar, 100), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.SYSTEM_CREATED, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            if (!string.IsNullOrEmpty(receiptValidationInfo.WhID))
            {
                paramArray[0].Value = receiptValidationInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = receiptValidationInfo.ReceiptValidationID;
            paramArray[2].Value = receiptValidationInfo.DefaultFlag;
            paramArray[3].Value = receiptValidationInfo.ReceiptWithoutPo;
            paramArray[4].Value = receiptValidationInfo.ReceiptWithoutLpn;
            paramArray[5].Value = receiptValidationInfo.ReceiptWithoutAsn;
            paramArray[6].Value = receiptValidationInfo.CommodityNotOnPo;
            paramArray[7].Value = receiptValidationInfo.CommodityNotOnAsn;
            paramArray[8].Value = receiptValidationInfo.IsQtyValidation;
            paramArray[9].Value = receiptValidationInfo.OveragePercent;
            if (!string.IsNullOrEmpty(receiptValidationInfo.Descr))
            {
                paramArray[10].Value = receiptValidationInfo.Descr;
            }
            else
            {
                paramArray[10].Value = DBNull.Value;
            }
            paramArray[11].Value = receiptValidationInfo.IsActive;
            paramArray[12].Value = receiptValidationInfo.SystemCreated;
            paramArray[13].Value = receiptValidationInfo.CreatedBy;
            paramArray[14].Value = receiptValidationInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateReceiptValidation(ReceiptValidationInfo receiptValidationInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                receiptValidationInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_ReceiptValidation_Parameters(receiptValidationInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_RECEIPT_VALIDATION.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateReceiptValidation(DataBase dataBase, DbTransaction tran, ReceiptValidationInfo receiptValidationInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                receiptValidationInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_ReceiptValidation_Parameters(receiptValidationInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_RECEIPT_VALIDATION.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

